using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class ChatImgItem : BaseChatElement
	{
		private const string TAG = "ChatImgItem";
		private new BaseChatContent ChatContentCtrl;
		private Image image_0;
		private TextBlock LoadingInfo;
		private Border LoadingOverlay;
		private Button fromButton;
		private HttpPicItem fromIcon;
		private string mImgSrc;
		private int mProgress = 100;
		private AppMsgInfo mMsginfo;
		public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(string), typeof(ChatImgItem), new PropertyMetadata(new PropertyChangedCallback(ChatImgItem.OnFromChanged)));
		private bool isAppInfoInit;
		private EventWatcher mWatcher;
		public string From
		{
			get
			{
				return (string)base.GetValue(ChatImgItem.FromProperty);
			}
			set
			{
				base.SetValue(ChatImgItem.FromProperty, value);
			}
		}
		public ChatImgItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.Item_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Item_Unloaded));
		}
		private void Item_Unloaded(object sender, RoutedEventArgs e)
		{
			this.GetAppInfoUnit();
		}
		private void Item_Loaded(object sender, RoutedEventArgs e)
		{
			this.GetAppInfoInit();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ChatContentCtrl = (base.GetTemplateChild("ChatContentCtrl") as BaseChatContent);
			this.image_0 = (base.GetTemplateChild("img") as Image);
			this.LoadingInfo = (base.GetTemplateChild("LoadingInfo") as TextBlock);
			this.LoadingOverlay = (base.GetTemplateChild("LoadingOverlay") as Border);
			this.ChatContentCtrl.add_Click(new RoutedEventHandler(this.ContentPanel_Click));
			this.fromButton = (base.GetTemplateChild("fromButton") as Button);
			if (this.fromButton != null)
			{
				this.fromButton.add_Click(new RoutedEventHandler(this.fromButton_Click));
			}
			this.fromIcon = (base.GetTemplateChild("picFromIcon") as HttpPicItem);
			this.RefreshProgress();
			this.RefreshImgSrc();
			this.RefreshFrom();
		}
		public Image GetImg()
		{
			return this.image_0;
		}
		private void ContentPanel_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
		}
		private void RefreshFrom()
		{
			if (this.fromButton != null)
			{
				this.fromButton.set_Visibility(1);
			}
			if (this.mMsginfo != null)
			{
				if ((long)this.mMsginfo.type == 2L)
				{
					AppInfo appinfo = AppInfoMgr.getAppinfo(this.mMsginfo.appid, MMAppInfoIconType.MMAPPINFO_ICONTYPE_SD);
					if (appinfo != null && !string.IsNullOrEmpty(appinfo.AppName) && !string.IsNullOrEmpty(appinfo.AppWatermarkUrl))
					{
						this.From = strings.Con_QQComeFrom + appinfo.AppName;
						if (!string.IsNullOrEmpty(appinfo.AppName))
						{
							if (this.fromButton != null)
							{
								this.fromButton.set_Visibility(0);
							}
							if (!string.IsNullOrEmpty(appinfo.AppWatermarkUrl) && this.fromIcon != null && this.fromIcon != null)
							{
								this.fromIcon.LoadImage(appinfo.AppWatermarkUrl, true);
							}
						}
						return;
					}
					return;
				}
			}
		}
		private void fromButton_Click(object sender, RoutedEventArgs e)
		{
			string strUrl = string.Concat(new string[]
			{
				"http://rd.wechatapp.com/app/source/?appid=",
				this.mMsginfo.appid,
				";version=24;lang=",
				R.GetCurrentLanguage(),
				";devicetype=wp7"
			});
			ToolFunction.GotoWebLinkInWebView(strUrl, GetA8KeyScene.MMGETA8KEY_SCENE_UNKNOW);
		}
		private void RefreshProgress()
		{
			if (this.LoadingOverlay == null)
			{
				return;
			}
			if (this.LoadingInfo == null)
			{
				return;
			}
			if (this.mProgress >= 0 && this.mProgress < 100)
			{
				this.LoadingOverlay.set_Visibility(0);
				this.LoadingInfo.set_Text(this.mProgress + "%");
				return;
			}
			this.LoadingOverlay.set_Visibility(1);
		}
		private void RefreshImgSrc()
		{
			if (this.image_0 == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mImgSrc))
			{
				this.SetEmptyImage();
				return;
			}
			BitmapImage bitmapImage = ImageCacheMgr.loadImage(this.mImgSrc, ImageCacheType.MsgImg);
			if (bitmapImage == null)
			{
				Log.smethod_1("ChatImgItem", "RefreshImgSrc:GetImageFromPath==null,mImgSrc:" + this.mImgSrc);
				this.SetEmptyImage();
				return;
			}
			this.image_0.set_Source(bitmapImage);
			this.image_0.set_Stretch(1);
			this.image_0.set_Width((double)bitmapImage.get_PixelWidth() * 1.2);
			this.image_0.set_Height((double)bitmapImage.get_PixelHeight() * 1.2);
			this.image_0.set_MaxWidth(240.0);
			this.image_0.set_MaxHeight(240.0);
		}
		private void SetEmptyImage()
		{
			this.image_0.set_Source(new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/empty_img_nor.png") as ImageSource);
			this.image_0.set_Width(60.0);
			this.image_0.set_Height(60.0);
		}
		public override void SetImgSrc(string ImgSrc)
		{
			this.mImgSrc = ImgSrc;
			this.RefreshImgSrc();
		}
		public override void SetProgress(int Progress)
		{
			this.mProgress = Progress;
			this.RefreshProgress();
		}
		public override void SetChatMsg(ChatMsg Msg)
		{
			base.SetChatMsg(Msg);
			if (this.chatMsg_0.IsAppMsg())
			{
				this.mMsginfo = AppMsgMgr.parseAppXml(Msg.strMsg);
			}
			else
			{
				this.mMsginfo = null;
			}
			this.RefreshFrom();
		}
		private static void OnFromChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void GetAppInfoInit()
		{
			if (this.isAppInfoInit)
			{
				return;
			}
			this.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GET_APPINFO_SUCCESS, this.mWatcher);
			this.isAppInfoInit = true;
		}
		private void GetAppInfoUnit()
		{
			if (!this.isAppInfoInit)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GET_APPINFO_SUCCESS, this.mWatcher);
			this.isAppInfoInit = false;
		}
		private void onEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			AppInfo appInfo = evtArgs.mObject as AppInfo;
			if (appInfo == null)
			{
				return;
			}
			if (this.mMsginfo == null)
			{
				return;
			}
			if (this.mMsginfo.appid != appInfo.AppID)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mMsginfo.fromUserName))
			{
				return;
			}
			if (!string.IsNullOrEmpty(appInfo.AppName))
			{
				this.From = strings.Con_QQComeFrom + appInfo.AppName;
				this.fromButton.set_Visibility(0);
				if (!string.IsNullOrEmpty(appInfo.AppWatermarkUrl) && this.fromIcon != null)
				{
					this.fromIcon.LoadImage(appInfo.AppWatermarkUrl, true);
				}
			}
		}
	}
}
